package com.thprojects.myphotomemories.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.thprojects.myphotomemories.R;
import com.thprojects.myphotomemories.controller.ContactsController;
import com.thprojects.myphotomemories.controller.DbController;
import com.thprojects.myphotomemories.controller.FSController;
import com.thprojects.myphotomemories.controller.LocationController;
import com.thprojects.myphotomemories.controller.OnGotContactsListener;
import com.thprojects.myphotomemories.controller.OnGotLocationListener;
import com.thprojects.myphotomemories.model.Album;
import com.thprojects.myphotomemories.model.Contact;

import exception.PhotoSourceNotFoundException;
import exception.SDNotReadyException;

public class PhotoDetailsView extends Activity{
	
	FSController mFSController;
	private String mFilePath;
	private Album mAlbum;
	
	TextView mNewPhotoFilePath;
	ImageView mNewPhoto;
	EditText mNewPhotoName;
	EditText mNewPhotoCaption;
	EditText mNewPhotoLocation;
	EditText mNewPhotoContacts;
	Button mNewPhotoChooseContacts;
	Button mNewPhotoGetLocation;
	
	Button mSaveNewPhoto;
	Button mCancelNewPhoto;
	
	Bitmap mImage;
	DbController mDbController;
	
	List<Contact> mContacts;
	ContactsController mContactsController;
	
	
	protected void getIntentInfo(){
		Intent i = getIntent();
        mFilePath = (String)i.getCharSequenceExtra("filePath");
        mAlbum = (Album)i.getParcelableExtra("album");
	}
	
	protected void setInfoInGui(){
		mFSController = FSController.getFSController(this);
		mNewPhotoFilePath.setText(mFilePath);

        Display d = getWindowManager().getDefaultDisplay();
        try {
			mImage = mFSController.decodeSampledBitmap(mFilePath, d.getWidth(), d.getWidth());
			mNewPhoto.setImageBitmap(mImage);
		} catch (PhotoSourceNotFoundException e) {
		} catch (SDNotReadyException e) {
		}
		
		mContacts = new ArrayList<Contact>();
		mContactsController = new ContactsController(PhotoDetailsView.this, new OnGotContactsListener() {
			public void onGotContacts(List<Contact> contacts) {
				mContacts = contacts;
				updateContactListView();
			}
		}); 
		updateContactListView();
	}
	
	protected void setSaveAndCancelListeners(OnClickListener saveListener, OnClickListener cancelListener){
		mSaveNewPhoto.setOnClickListener(saveListener);
		mCancelNewPhoto.setOnClickListener(cancelListener);
	}
	
	protected void setSaveAndCancelListeners(){
		setSaveAndCancelListeners(
        	new OnClickListener() {
				public void onClick(View v) {
					mDbController.savePhoto(mFilePath, mNewPhotoName.getText().toString(), mNewPhotoCaption.getText().toString(), mNewPhotoLocation.getText().toString(), mAlbum, mContacts);
					finish();
				}
        	},
        	new OnClickListener() {		
				public void onClick(View v) {
					finish();
				}
        	}
        );
	}
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo_details_view);
        
        getIntentInfo();
        
        //get GUI components
        mNewPhotoFilePath = (TextView)findViewById(R.id.new_photo_file_path);
        mNewPhoto = (ImageView)findViewById(R.id.new_photo);
        mNewPhotoName = (EditText)findViewById(R.id.new_photo_name);
        mNewPhotoCaption = (EditText)findViewById(R.id.new_photo_caption);
        mNewPhotoLocation = (EditText)findViewById(R.id.new_photo_location);
        mNewPhotoGetLocation = (Button)findViewById(R.id.new_photo_get_location);
        mNewPhotoContacts = (EditText)findViewById(R.id.new_photo_contacts);
        mNewPhotoChooseContacts = (Button)findViewById(R.id.new_photo_choose_contacts);
            
		
		mDbController = DbController.getDbController(this);
		
        mSaveNewPhoto = (Button)findViewById(R.id.save_new_photo);
        mCancelNewPhoto = (Button)findViewById(R.id.cancel_new_photo);
        
        setSaveAndCancelListeners();
        
        mNewPhotoGetLocation.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				LocationController lc = new LocationController(PhotoDetailsView.this, new OnGotLocationListener() {	
					public void onGotLocation(String location) {
						mNewPhotoLocation.setText(location);
					}
				});
				lc.localize();	
			}
		});
        
        
        mNewPhotoChooseContacts.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				
				mContactsController.askContacts();
			}
		});
        
        setInfoInGui();
        
		
    }
    
    protected void updateContactListView(){
    	if (mContacts.size() == 0){
    		mNewPhotoContacts.setText("No contact tagged");
    		return;
    	}
    	mNewPhotoContacts.setText("");
    	for (int i=0; i<mContacts.size()-1; i++){
    		mNewPhotoContacts.append(mContacts.get(i).getDisplayName()+", ");
    	}
    	mNewPhotoContacts.append(mContacts.get(mContacts.size()-1).getDisplayName()); 
    }
    

}
